package com.pureut.storage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pureut.storage.domain.WmsStockInfo;
import com.pureut.storage.dto.MaterialSplittingDto;
import com.pureut.storage.dto.NotAverageVo;
import com.pureut.storage.mapper.MaterialSplittingMapper;
import com.pureut.storage.mapper.WmsStockInfoMapper;
import com.pureut.storage.service.MaterialSplittingService;
import com.pureut.storage.service.WmsStockInfoService;
import com.pureut.storage.vo.SplittingVo;
import com.sale.common.core.domain.R;
import com.sale.common.core.utils.bean.BeanUtils;
import com.sale.system.api.RemoteCodeService;
import com.sale.system.api.RemoteUnitService;
import com.sale.system.api.model.MaterialUnitDto;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 物料拆分实现层
 */
@Service
public class MaterialSplittingServiceImpl implements MaterialSplittingService {

    @Resource
    MaterialSplittingMapper materialSplittingMapper;

    @Resource
    WmsStockInfoMapper wmsStockInfoMapper;

    @DubboReference
    RemoteCodeService remoteCodeService;

    @DubboReference
    RemoteUnitService remoteUnitService;

    /**
     * 物料拆分通过物料号码查询出物料信息
     *
     * @param materialNumber
     * @return
     */
    @Override
    public MaterialSplittingDto byMaterialNumber(String materialNumber) {
        //单位集合
        Map<String, MaterialUnitDto> unitByCodeToMap = remoteUnitService.getUnitByCodeToMap();
        MaterialSplittingDto materialSplittingDto = materialSplittingMapper.ByMaterialNumber(materialNumber);
        //物料单位
        if (materialSplittingDto.getMaterialUnit() != null) {
            materialSplittingDto.setMaterialUnit(unitByCodeToMap.get(materialSplittingDto.getMaterialUnit()).getUnitName());
        }
        return materialSplittingDto;
    }

    /**
     * 平均拆分打印
     */
    @Override
    public boolean average(SplittingVo splittingVo) throws Exception {
        //通过物料号码查询出库存信息
        WmsStockInfo wmsStockInfo = wmsStockInfoMapper.selectOne(new QueryWrapper<WmsStockInfo>().lambda().eq(WmsStockInfo::getWsiMaterialNumber, splittingVo.getMaterialNumber()));

        //把这个物料号码对应的库存拆分成两个不同的号码
        List<WmsStockInfo> wmsStockInfos = new ArrayList<>();
        for (int i = 0; i < splittingVo.getSplittingNum(); i++) {
            WmsStockInfo wmsStockInfo1 = new WmsStockInfo();
            BeanUtils.copyProperties(wmsStockInfo,wmsStockInfo1,"id");
            wmsStockInfo1.setWsiNetWeight(Double.parseDouble(splittingVo.getAveragePackage()));

            //号码生成
            String cgrkdynum = remoteCodeService.splitList("CGRKDYNUM");
            wmsStockInfo1.setWsiMaterialNumber(cgrkdynum);
            wmsStockInfos.add(wmsStockInfo1);
        }
        wmsStockInfoMapper.insertBatch(wmsStockInfos);
        //删除该物料号码
        return wmsStockInfoMapper.deleteById(wmsStockInfo)>0;
    }

    /**
     * 非平均拆分打印
     * @param splittingVo
     * @return
     */
    @Override
    public boolean notAverage(SplittingVo splittingVo) throws Exception {
        //通过物料号码查询出库存信息
        WmsStockInfo wmsStockInfo = wmsStockInfoMapper.selectOne(new QueryWrapper<WmsStockInfo>().lambda().eq(WmsStockInfo::getWsiMaterialNumber, splittingVo.getMaterialNumber()));

        //把这个物料号码对应的库存拆分成两个不同的号码
        List<WmsStockInfo> wmsStockInfos = new ArrayList<>();
        for (NotAverageVo notAverageVo : splittingVo.getArray()) {
            WmsStockInfo wmsStockInfo1 = new WmsStockInfo();
            BeanUtils.copyProperties(wmsStockInfo,wmsStockInfo1,"id");
            wmsStockInfo1.setWsiNetWeight(Double.parseDouble(notAverageVo.getNum()));

            //号码生成
            String cgrkdynum = remoteCodeService.splitList("CGRKDYNUM");
            wmsStockInfo1.setWsiMaterialNumber(cgrkdynum);
            wmsStockInfos.add(wmsStockInfo1);
        }

        wmsStockInfoMapper.insertBatch(wmsStockInfos);
        //删除该物料号码
        return wmsStockInfoMapper.deleteById(wmsStockInfo)>0;
    }
}
